<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\FlashModel;

class Flash extends Common
{
	/**
	*	轮播图列表
	*/
	public function index()
	{
		$data = $this->request->param();
		$FlashModel = new FlashModel();
		$goods = $FlashModel->getGoods();
		$list = $FlashModel->getFlash($data);
		$page = $list->render();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('goods',$goods);
		$this->assign('gid',isset($data['gid']) ? $data['gid'] : '');
		$this->assign('status',isset($data['status']) ? $data['status'] : '');
		return $this->fetch();
	}

	/**
	*	添加轮播图
	*/
	public function add()
	{
		$FlashModel = new FlashModel();
		$goods = $FlashModel->getGoods();
		$this->assign('goods',$goods);
		return $this->fetch();
	}

	/**
	*	添加轮播图提交
	*/
	public function addPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$result = $this->validate($data, 'FlashValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$FlashModel = new FlashModel();

	        $list = $FlashModel->addFlash($data);

	        if ($list){
	        	$this->success('添加成功');
	        }else{
	        	$this->error('添加失败');
	        }
        }
	}

	/**
	*	修改轮播图
	*/
	public function edit()
	{
		$data = $this->request->param();
		$FlashModel = new FlashModel();
		$goods = $FlashModel->getGoods();
		$list = $FlashModel->getOne($data['id']);
		$this->assign('goods',$goods);
		$this->assign('list',$list);
		return $this->fetch();
	}

	/**
	*	修改轮播图提交
	*/
	public function editPost()
	{
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$result = $this->validate($data, 'FlashValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$FlashModel = new FlashModel();

	        $list = $FlashModel->editFlash($data['id'],$data);

	        $this->success('修改成功');
        }
	}

	/**
	*	删除轮播图
	*/
	public function delete()
	{
		$data = $this->request->param();
		if($data){
			$FlashModel = new FlashModel();
			$list = $FlashModel->deleteFlash($data['id']);
			if($list === 1){
				$this->success('删除成功',url('/admin/Flash/index'));

			}elseif($list === 2){
				$this->error('该数据已上架,不能删除',url('/admin/Flash/index'));
			}
		}
	}

	/**
	*	批量发布
	*/
	public function racking()
	{
		$data = $this->request->param();
		$FlashModel = new FlashModel();
		$list = $FlashModel->racking($data);
		if($list){
			return returnJson($code=1,$msg='修改成功',$list);
		}else{
			return returnJson($code=1,$msg='修改成功',$list);
		}
	}

	/**
	*	批量发布
	*/
	public function rack()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$FlashModel = new FlashModel();
			$list = $FlashModel->rack($data);
			if($list){
				return returnJson($code=1,$msg='修改成功',$list);
			}else{
				return returnJson($code=1,$msg='修改成功',$list);
			}
		}
	}
}